package com.example.studentmanagmentsystem;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.util.ArrayList;
import java.util.List;

public class LoginActivity extends BaseActivity {
    private EditText accountEdit;
    private EditText passwordEdit;
    private Button login;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        accountEdit = (EditText) findViewById(R.id.account);
        passwordEdit = (EditText) findViewById(R.id.password);
        login = (Button) findViewById(R.id.login);

        //save("\nguest-guest");

        List<String> list = loadUser();
//        list.add("admin-admin");
//        list.add("guest-guest");
        list.add("hello-hello");
        login.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String account = accountEdit.getText().toString();
                String password = passwordEdit.getText().toString();
                //验证账号密码
                int flag = 0;
                for (int i = 0; i < list.size(); i++) {
                    if (account.equals(list.get(i).split("-")[0]) && password.equals(list.get(i).split("-")[1])) {//验证成功则跳转到MainActivity
                        Intent intent = new Intent(com.example.studentmanagmentsystem.LoginActivity.this, MainActivity.class);
                        startActivity(intent);
                        finish();
                        flag = 1;
                    }
                }
                if (flag == 0) {
                    //不成功则提示错误
                    Toast.makeText(com.example.studentmanagmentsystem.LoginActivity.this, "账号或密码输入错误", Toast.LENGTH_SHORT).show();
                }


            }
        });
    }


    public List<String> loadUser() {//读取data文件
        FileInputStream in = null;
        BufferedReader reader = null;
        //strings对象不能为null值必须初始化, 不然会报错
        List<String> strings = new ArrayList<>();
        try {
            in = openFileInput("data");
            reader = new BufferedReader(new InputStreamReader(in));
            String line = "";
            while ((line = reader.readLine()) != null) {
                strings.add(line);

            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (reader != null) {
                try {
                    reader.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return strings;
    }

    public void save(String inputText) {//写入data文件
        FileOutputStream outputStream = null;
        BufferedWriter writer = null;
        try {
            outputStream = openFileOutput("data", Context.MODE_APPEND);
            writer = new BufferedWriter(new OutputStreamWriter(outputStream));
            writer.write(inputText);
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if (writer != null) {
                    writer.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

}